﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AGameOfThrones.board
{
    /// <summary>
    /// Interaction logic for board.xaml
    /// </summary>
    public partial class board : UserControl
    {

        /// <summary>
        /// The point that was clicked relative to the content that is contained within the ZoomAndPanControl.
        /// </summary>
        private Point origMouseDownPoint;

        public board()
        {
            InitializeComponent();
        }

        // accessor
        public map Map
        {
            get { return m_map; }
        }
        
        private void zoomAndPanControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            common.zoomandpan control = sender as common.zoomandpan;
            origMouseDownPoint = e.GetPosition(content);
            zoomAndPanControl.CaptureMouse();
        }

        private void zoomAndPanControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            zoomAndPanControl.ReleaseMouseCapture();
        }

        /// <summary>
        /// Event raised on mouse move in the ZoomAndPanControl.
        /// </summary>
        private void zoomAndPanControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (zoomAndPanControl.IsMouseCaptured)
            {
                //
                // The user is left-dragging the mouse.
                // Pan the viewport by the appropriate amount.
                //
                common.zoomandpan control = sender as common.zoomandpan;
                Point curMousePoint = e.GetPosition(content);
                Vector dragOffset = curMousePoint - origMouseDownPoint;

                zoomAndPanControl.ContentOffsetX -= dragOffset.X;
                zoomAndPanControl.ContentOffsetY -= dragOffset.Y;

            }
        }

        /// <summary>
        /// Event raised by rotating the mouse wheel
        /// </summary>
        private void zoomAndPanControl_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            if (e.Delta > 0)
            {
                Point curMousePoint = e.GetPosition(content);
                ZoomIn(curMousePoint);
            }
            else if (e.Delta < 0)
            {
                Point curMousePoint = e.GetPosition(content);
                ZoomOut(curMousePoint);
            }
            e.Handled = true;
        }

        /// <summary>
        /// Zoom the viewport out, centering on the specified point (in content coordinates).
        /// </summary>
        private void ZoomOut(Point contentZoomCenter)
        {
            zoomAndPanControl.ZoomAboutPoint(zoomAndPanControl.ContentScale - 0.1, contentZoomCenter);
        }

        /// <summary>
        /// Zoom the viewport in, centering on the specified point (in content coordinates).
        /// </summary>
        private void ZoomIn(Point contentZoomCenter)
        {
            zoomAndPanControl.ZoomAboutPoint(zoomAndPanControl.ContentScale + 0.1, contentZoomCenter);
        }
    }
}
